import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import httpServer from './request'
import { HandledPhoto, getUserId } from './unit'
import { Message, Popover, Loading} from 'element-ui'
import VueLazyLoad from 'vue-lazyload'
getUserId();
Vue.config.productionTip = false
if(process.env.NODE_ENV !== 'production') {
  Vue.use(Loading.directive);
}
Object.defineProperties(Vue.prototype, {
  $http: { value: httpServer},
  $message: { value: Message},
  $loading: { value: Loading},
  $addRate: {
    value(el, text, value) {
      let rate = document.getElementsByClassName('ff-rate-tip-in-body')[0];
      let parent = el.target.parentNode;
      let x = el.pageX;
      let y = el.pageY;
      // 移入到相同位置时只需显示
      if(parent.dataset.id == value && rate && rate.innerText === text) return rate && (rate.style.display = 'block');
      // 所有rate的tip只创建一次, 以后全复用
      if(!rate){
        rate = document.createElement('div');
        rate.setAttribute('class', 'ff-rate-tip-in-body');
      }
      // 在父节点上设置移入到了第几个星星,为了下次再移入到原星星时不需再设置位置
      parent.setAttribute('data-id', value)
      rate.setAttribute('style', `left: ${x + 10}px; top: ${y + 15}px; transform-origin: center top 0px; `);
      rate.innerText = text;
      if(rate) document.body.appendChild(rate);
    }
  },
  $removeRate: {
    value () {
      let rate = document.getElementsByClassName('ff-rate-tip-in-body')[0];
      rate.style.display = 'none';
    }
  }
})
Vue.use(Popover)
Vue.use(VueLazyLoad, {
  loading: require('@/assets/img/default.png')  //这个就是你本地图片的地址
})
router.beforeEach((to, from, next) => {
  httpServer({
    url: '/api/SelPro/GetStrInfo',
    method: 'post',
    data: {
      strId: store.state.userId
    }
  }).then(res => {
    let data = res.data;
    if(data.code === 1) {
      store.commit('serUserInform', data.data)
      if(to.fullPath === '/') {
        next({
          path: '/recommend'
        });
      }else {
        store.commit('setRecordRoute', {
          path: to.fullPath,
          label: to.meta.label === 'dynamic'?to.query.sku : to.meta.label
        });
        next()
      }
    }
  })
})
Vue.mixin({
  methods: {
    HandledPhoto
  }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
